<?php
//添加缩略图支持
if ( function_exists( 'add_theme_support' ) ) {
	add_theme_support( 'post-thumbnails' );
	add_image_size( 'sidebar-post-thumbnail', 245, 245, true ); //侧边栏
}

//去除头部多余信息
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'start_post_rel_link', 10, 0);
remove_action('wp_head', 'parent_post_rel_link', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);
remove_filter('the_content', 'wptexturize');

//为编辑器增加按钮
function add_more_buttons($buttons) { 
$buttons[] = 'hr'; 
$buttons[] = 'sub'; 
$buttons[] = 'sup'; 
$buttons[] = 'fontselect'; 
$buttons[] = 'fontsizeselect'; 
$buttons[] = 'cleanup'; 
$buttons[] = 'styleselect'; 
return $buttons; 
}
add_filter("mce_buttons_3", "add_more_buttons");

//注册菜单
if( function_exists('register_nav_menus') ){
	register_nav_menus(
		array(
			'uctheme-menu' => '主导航菜单',
			'left-menu' => '左侧悬挂菜单',
		    'hot-menu' => '热门活动菜单',
		)
	);
}

//注册小工具
function ucthemeWidgetRegister(){
	//注册侧边栏
	if ( function_exists('register_sidebar') ) {
	register_sidebar(array(
		'name' => '侧边栏',
		'description' => '文章详细页面侧边栏',
		'before_widget' => '<div id="%1$s" class="rightBox %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<div class="titleStyle">',
		'after_title' => '</div>',
	));
	/*register_sidebar(array(
		'name' => '侧边栏滑动部分',
		'description' => '侧边栏滑动部分，建议只放一两个小工具',
		'id'=>'slider',
		'before_widget' => '<div id="%1$s" class="rightBox %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<div class="titleStyle">',
		'after_title' => '</div>',
	));*/

	}
	
	register_widget('Onecolumnsads');
	register_widget('NineSquare');
	register_widget('HotPost');
}
add_action('widgets_init','ucthemeWidgetRegister');

/********添加query变量************/
function smzdm_query_vars($public_query_vars) {
	$public_query_vars[] = 'my_custom_page_type'; 
	$public_query_vars[] = 'step'; 
	$public_query_vars[] = 'my_second_page';
	$public_query_vars[] = 'third_page';
	$public_query_vars[] = 'username';
	$public_query_vars[] = 'pid';
	$public_query_vars[] = 'post_id';
	
	return $public_query_vars;  
}
/********锁定后台****/
function redirect_user_login() {
	global $current_user,$pagenow;
	get_currentuserinfo();
	$valid_pages = array('admin-ajax.php', 'async-upload.php', 'media-upload.php');
	if( ! current_user_can('level_10') && !in_array( $pagenow, $valid_pages ) ){
		wp_redirect( smzdm_my_account_link() ); exit;
	}
}
add_action('admin_init','redirect_user_login');
function smzdm_my_account_link(){
	return site_url()."/my-account";		
}
function smzdm_post_new_link(){
	return site_url()."/post-new";		
}

/************重写规则*************/
function smzdm_rewrite_rules( $wp_rewrite ){
$new_rules = array(
'go/?([0-9]{1,})/?$' => 'index.php?my_custom_page_type=go_page&pid='.$wp_rewrite->preg_index(1),
'user-profile/([^/]+)/?$' => 'index.php?my_custom_page_type=user_profile&username='.$wp_rewrite->preg_index(1),
'user-profile/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?my_custom_page_type=user_profile&username='.$wp_rewrite->preg_index(1).'&paged='.$wp_rewrite->preg_index(2),
'post_new/?$' => 'index.php?my_custom_page_type=post_new'.$wp_rewrite->preg_index(1),
'post-new?$' => 'index.php?my_custom_page_type=post-new&step='.$wp_rewrite->preg_index(1),
'my-account/?$' => 'index.php?my_custom_page_type=my_account&step=1',
'my-account/([^/]+)/?$' => 'index.php?my_custom_page_type=my_account&paged=1&my_second_page='.$wp_rewrite->preg_index(1),
'my-account/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?my_custom_page_type=my_account&my_second_page='.$wp_rewrite->preg_index(1).'&paged='.$wp_rewrite->preg_index(2),
'my-account/([^/]+)/my-page/([^/]+)/?$' => 'index.php?my_custom_page_type=my_account&my_second_page='.$wp_rewrite->preg_index(1).'&third_page='.$wp_rewrite->preg_index(2),
'my-account/([^/]+)/?([0-9]{1,})/?$' => 'index.php?my_custom_page_type=my_account&my_second_page='.$wp_rewrite->preg_index(1).'&post_id='.$wp_rewrite->preg_index(2),
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

/************模板载入规则****************/
function smzdm_template_redirect(){
	global $wp,$wp_query,$wp_rewrite;
	if( !isset($wp_query->query_vars['my_custom_page_type']) )
		return;
	$reditect_page =  $wp_query->query_vars['my_custom_page_type'];
	
	if ($reditect_page == "my_account"){
		include(THEMEUSER.'my-account.php');
		die();
	}
	if ($reditect_page == "go_page"){
		include(get_template_directory().'/go.php');
		die();
	}

	if ($reditect_page == "user_profile"){
		include(THEMEUSER.'user-profile.php');
		die();
	}
	if ($reditect_page == "post-new"){
		include(THEMEUSER.'page-newpost.php');
		die();
	}
}
/*********更新重写规则***************/
function smzdm_flush_rewrite_rules() {
	global $pagenow, $wp_rewrite;

	if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) )
		$wp_rewrite->flush_rules();
}
add_action( 'load-themes.php', 'smzdm_flush_rewrite_rules' );

//session_start();
add_action('generate_rewrite_rules', 'smzdm_rewrite_rules' );
add_action('query_vars', 'smzdm_query_vars');  
add_action("template_redirect", 'smzdm_template_redirect');
show_admin_bar(false);

function dev4press_debug_page_request() {   
  global $wp, $template;   
  define("D4P_EOL", "\r\n");   
  echo '<!-- Request: ';   
  echo empty($wp->request) ? "None" : esc_html($wp->request); //输出请求   
  echo ' -->'.D4P_EOL;   
  echo '<!-- Matched Rewrite Rule: ';   
  echo empty($wp->matched_rule) ? None : esc_html($wp->matched_rule); //输出翻译   
  echo ' -->'.D4P_EOL;   
  echo '<!-- Matched Rewrite Query: ';   
  echo empty($wp->matched_query) ? "None" : esc_html($wp->matched_query); //输出查询参数   
  echo ' -->'.D4P_EOL;   
  echo '<!-- Loaded Template: ';   
  echo basename($template); //输出模板名称   
  echo ' -->'.D4P_EOL;   
}

function dev4press_debug_rewrite_rules() {   
  global $wp_rewrite;   
  echo '<div>';   
  if (!empty($wp_rewrite->rules)) { //如果存在url翻译   
    echo '<h5>Rewrite Rules</h5>';   
    echo '<table><thead><tr>';   
    echo '<td>Rule</td><td>Rewrite</td>';   
    echo '</tr></thead><tbody>';   
    foreach ($wp_rewrite->rules as $name => $value) { //输出翻译规则   
      echo '<tr><td>'.$name.'</td><td>'.$value.'</td></tr>';   
    }   
    echo '</tbody></table>';   
  } else {   
    echo 'No rules defined.';   
  }   
  echo '</div>';   
} 

/********************************/
function wp_register_uctheme( $before = '<li>', $after = '</li>', $echo = true ) {
	if ( ! is_user_logged_in() ) {
		if ( get_option('users_can_register') )
		$link = $before . '<a class="regBtn" href="' . site_url('wp-login.php?action=register', 'login') . '">' .  '</a>' . $after;
		else
		$link = '';
		} else {
		$link = $before . '<a   class="regBtn" href="' . admin_url() . '">' . '</a>' . $after;
	}
	
	if ( $echo )
	echo apply_filters('register', $link);
	else
	return apply_filters('register', $link);
}
?>